using System;
using System.Runtime.InteropServices;

namespace Pegasus.Runtime.InteropServices
{
	/// <summary>
	/// Extends the system Marshal class to provide additional funcationality.
	/// </summary>
	public static class MarshalEx
	{
		/// <summary>
		/// Gets the GUID that is set on an object or interface using the [GuidAttribute].
		/// </summary>
		/// <param name="obj">The object to get the guid from.</param>
		/// <returns>The Guid.</returns>
		public static Guid GetGuid( object obj )
		{
			return GetGuid( obj.GetType() );
		}

		/// <summary>
		/// Gets the GUID that is set on an object or interface using the [GuidAttribute].
		/// </summary>
		/// <param name="type">The type to get the guid from.</param>
		/// <returns>The Guid.</returns>
		public static Guid GetGuid( Type type )
		{
			object[] guids = type.GetCustomAttributes( typeof( GuidAttribute ), false );
			if( guids.Length > 0 )
			{
				GuidAttribute attrib = (GuidAttribute) guids[ 0 ];
				return new Guid( attrib.Value );
			}

			return Guid.Empty;
		}
	}
}
